<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>取得服务器响应一个 HTTP 请求所发送的所有标头</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.get-headers" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">get_headers</h1>
  <p class="verinfo">(PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">get_headers</span> &mdash; <span class="dc-title">取得服务器响应一个 HTTP 请求所发送的所有标头</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.get-headers-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>get_headers</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$url</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$format</code><span class="initializer"> = 0</span></span>
  ] ) : <span class="type">array</span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>get_headers()</strong></span> 返回一个数组，包含有服务器响应一个
   HTTP 请求所发送的标头。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.get-headers-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">url</code></dt>

     <dd>

      <p class="para">
       目标 URL。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">format</code></dt>

     <dd>

      <p class="para">
        如果将可选的 <code class="parameter">format</code> 参数设为 1，则
   <span class="function"><strong>get_headers()</strong></span> 会解析相应的信息并设定数组的键名。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.get-headers-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回包含有服务器响应一个
   HTTP 请求所发送标头的索引或关联数组，如果失败则返回 <strong><code>FALSE</code></strong>。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.get-headers-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.1.3</td>
       <td>
        自 PHP 5.1.3 起本函数使用默认的流上下文，其可以用 <span class="function"><a href="stream_context_get_default.html" class="function">stream_context_get_default()</a></span>
    函数设定和修改。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.get-headers-examples">
 <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4883">
    <p><strong>Example #1 <span class="function"><strong>get_headers()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$url&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'http://www.example.com'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">get_headers</span><span style="color: #007700">(</span><span style="color: #0000BB">$url</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">get_headers</span><span style="color: #007700">(</span><span style="color: #0000BB">$url</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; HTTP/1.1 200 OK
    [1] =&gt; Date: Sat, 29 May 2004 12:28:13 GMT
    [2] =&gt; Server: Apache/1.3.27 (Unix)  (Red-Hat/Linux)
    [3] =&gt; Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
    [4] =&gt; ETag: &quot;3f80f-1b6-3e1cb03b&quot;
    [5] =&gt; Accept-Ranges: bytes
    [6] =&gt; Content-Length: 438
    [7] =&gt; Connection: close
    [8] =&gt; Content-Type: text/html
)

Array
(
    [0] =&gt; HTTP/1.1 200 OK
    [Date] =&gt; Sat, 29 May 2004 12:28:14 GMT
    [Server] =&gt; Apache/1.3.27 (Unix)  (Red-Hat/Linux)
    [Last-Modified] =&gt; Wed, 08 Jan 2003 23:11:55 GMT
    [ETag] =&gt; &quot;3f80f-1b6-3e1cb03b&quot;
    [Accept-Ranges] =&gt; bytes
    [Content-Length] =&gt; 438
    [Connection] =&gt; close
    [Content-Type] =&gt; text/html
)
</pre></div>
    </div>
   </div>
   <div class="example" id="example-4884">
    <p><strong>Example #2 <span class="function"><strong>get_headers()</strong></span> using HEAD example</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;By&nbsp;default&nbsp;get_headers&nbsp;uses&nbsp;a&nbsp;GET&nbsp;request&nbsp;to&nbsp;fetch&nbsp;the&nbsp;headers.&nbsp;If&nbsp;you<br />//&nbsp;want&nbsp;to&nbsp;send&nbsp;a&nbsp;HEAD&nbsp;request&nbsp;instead,&nbsp;you&nbsp;can&nbsp;do&nbsp;so&nbsp;using&nbsp;a&nbsp;stream&nbsp;context:<br /></span><span style="color: #0000BB">stream_context_set_default</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'http'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'method'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'HEAD'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;)<br />);<br /></span><span style="color: #0000BB">$headers&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">get_headers</span><span style="color: #007700">(</span><span style="color: #DD0000">'http://example.com'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.get-headers-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="apache_request_headers.html" class="function" rel="rdfs-seeAlso">apache_request_headers()</a> - 获取全部 HTTP 请求头信息</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>